document.addEventListener('DOMContentLoaded', function () {
    // 背景图片配置
    const backgrounds = [
        public_url + '/default/images/login_bg.jpg'
        //public_url + '/default/images/login_bg2.jpg',
        //public_url + '/default/images/login_bg3.jpg'
    ];

    const body = document.body;
    const preloadArea = document.getElementById('preload-area');

    // 1. 预加载图片
    function preloadImages() {
        backgrounds.forEach(url => {
            const img = new Image();
            img.src = url;
            preloadArea.appendChild(img);
        });
    }

    // 2. 创建背景层
    function createBackgroundLayers() {
        backgrounds.forEach((url, index) => {
            const layer = document.createElement('div');
            layer.className = 'background';
            layer.style.backgroundImage = `url(${url})`;

            if (index === 0) {
                layer.classList.add('active');
            }

            body.insertBefore(layer, body.firstChild);
        });
    }

    // 3. 切换背景
    function switchBackground() {
        const layers = document.querySelectorAll('.background');
        let currentActive = document.querySelector('.background.active');
        let nextIndex = Array.from(layers).indexOf(currentActive) + 1;

        if (nextIndex >= layers.length)
            nextIndex = 0;

        currentActive.classList.remove('active');
        layers[nextIndex].classList.add('active');
    }

    // 初始化
    preloadImages();
    createBackgroundLayers();

    // 每3秒切换一次
    setInterval(switchBackground, 3000);
});